package com.hp.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hp.entity.User;
import com.hp.entity.dto.UserDto;
import com.hp.service.UserService;
import com.hp.utils.Page;
import com.hp.utils.PasswordUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "UserServlet",value = "/userServlet")
public class UserServlet extends HttpServlet {
    private ObjectMapper mapper=new ObjectMapper();
    private UserService userService=new UserService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        String method = request.getParameter("method");
        if(method!=null&&method.equals("login")){
            doLogin(request,response);
        }else if(method!=null&&method.equals("logout")){
            logout(request,response);
        }else if(method!=null&&method.equals("page")){
            getUserListByPage(request,response);
        }else if(method!=null&&method.equals("addUser")){
            addUser(request,response);
        }else if(method!=null&&method.equals("getDocterList")){
            getDocterList(request,response);
        }else if(method!=null&&method.equals("editUserInfo")){
            editUserInfo(request,response);
        }else if(method!=null&&method.equals("updateUserHeadUrl")){
            updateUserHeadUrl(request,response);
        }

    }
    private void updateUserHeadUrl(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Map<String,Object> map=new HashMap<>();
        String headUrl = request.getParameter("headUrl");
        String id = request.getParameter("id");
        if(userService.updateUserHeadUrl(headUrl,id)){
            map.put("code",1001);
            map.put("msg","修改个人图像成功");
        }else{
            map.put("code",1002);
            map.put("msg","编辑个人图像失败");
        }
        String json=mapper.writeValueAsString(map);
        response.getWriter().print(json);
    }
    private void editUserInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Map<String,Object> map=new HashMap<>();
        String password = request.getParameter("password");
        String headUrl = request.getParameter("headUrl");
        String id = request.getParameter("id");
        String salt = PasswordUtil.salt();
        String dbPassword=PasswordUtil.encode(password,salt);
        User user=new User();
        user.setId(Integer.parseInt(id));
        user.setHeadUrl(headUrl);
        user.setSalt(salt);
        user.setPassword(dbPassword);
        if(userService.editUserInfo(user)){
            map.put("code",1001);
            map.put("msg","编辑个人信息成功");
        }else{
            map.put("code",1002);
            map.put("msg","编辑个人信息失败");
        }
        String json=mapper.writeValueAsString(map);
        response.getWriter().print(json);
    }

    //查询门诊医生
    private void getDocterList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Map<String,Object> map=new HashMap<>();
        String sign = request.getParameter("sign");
        String departmentId = request.getParameter("departmentId");
        List<User> docterList=null;
        if(sign!=null){
            docterList=userService.getDocterList(sign,departmentId);
        }else {
            docterList=userService.getDocterList(departmentId);
        }
        map.put("docterList",docterList);
        String json=mapper.writeValueAsString(map);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        response.getWriter().print(json);
    }

    private void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String,Object> map=new HashMap<>();
        User user=new User();
        try {
            BeanUtils.populate(user,request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //设置其他字段值
        Date currentDate=new Date();
        user.setCreatedTime(currentDate);
        user.setUpdatedTime(currentDate);
        //加密加盐操作
        String salt = PasswordUtil.salt();
        user.setSalt(salt);
        String password = request.getParameter("password");
        String dbPassword=PasswordUtil.encode(password,salt);
        user.setPassword(dbPassword);
        //添加用户
        if(userService.addUser(user)){
            map.put("code",1006);
            map.put("msg","添加用户成功");
        }else{
            map.put("code",1007);
            map.put("msg","添加用户失败");
        }
        String json=mapper.writeValueAsString(map);
        response.getWriter().print(json);
    }

    private void getUserListByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String,Object> map=new HashMap<>();
        String currentPage = request.getParameter("currentPage");

        String pageSize = request.getParameter("pageSize");

        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        Page page=userService.getUserListByPage(map);
        map.put("page",page);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        String json=mapper.writeValueAsString(page);
        response.getWriter().print(json);
    }
    private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String,Object> map=new HashMap<>();
        map.put("code",1003);
        request.getSession().removeAttribute("loginUser");
        String json=mapper.writeValueAsString(map);
        response.getWriter().print(json);
    }
    private void doLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String,Object> map=new HashMap<>();
        String account = request.getParameter("account");

        String password = request.getParameter("password");
        UserDto dto = null;
        dto = userService.login(account);
        if(dto!=null){
           if(PasswordUtil.match(password, dto.getPassword())){
               dto.setPassword(null);
               dto.setSalt(null);
               map.put("code",1001);
               map.put("loginUser",dto);
               request.getSession().setAttribute("loginUser",dto);
           }else {
               map.put("code",1002);
           }
        }else {
            map.put("code",1002);
        }
        String json=mapper.writeValueAsString(map);
        response.getWriter().print(json);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
